﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics.Contracts;

namespace DataStructure.Interview
{
    public class Question51
    {
        public static int Replace(int N, int M, int i, int j)
        {
            int allone = ~0;
            int left = allone - (1 << j -1); 
            //11111111111111111
            //00000000111111111
            //11111111000000000
            int right = 1 << i -1;
            //00000000000011111                            
            int temp = left | right;
            //11111111000011111                         
            return (M << i) | (N&temp);
            //00000000XXXXX0000

            //YYYYYYYYYYYYYYYYY
            //YYYYYYYY0000YYYYY
            //00000000XXXXX0000
            //YYYYYYYYXXXXXYYYY
            }
    }
}
